<?php

include 'D:/dingjiangbbs/common/home.php';
//屏蔽notice错误
error_reporting(E_ALL ^ E_NOTICE);
session_start();



if ( $_COOKIE['loginAfter'] != 1) {
	exit('请先登录在进行发表');
}
//var_dump($_POST);
//限制发帖的内容
if (empty(trim($_POST['content']))) {
	exit('你发表的内容不能为空或空字符');
}

//强制限制条件（否则报错）
if (empty(trim($_POST['rate']))) {
	exit('价格不得为空或空字符，谢谢！');
}

//var_dump($_POST);
//获取发帖信息
$title    = trim($_POST['title']);
$title    = empty($title)?'无题':$title;
$content  = $_POST['content'];
$classid  = $_POST['classid'];
//var_dump($classid);
$rate     = trim($_POST['rate']);

//var_dump($rate);
$verify   = trim($_POST['verify']);

//uid为cookie值【在登录处定义】
$authorid = $_COOKIE['uid'];
//var_dump($authorid);

//验证码防止注水
if ($verify !==  $_SESSION['yzm']) {

	exit('验证码输入有误');
}

//获取发帖人ip
$ip = $_SERVER['REMOTE_ADDR'];

if ($ip = '::1') {

	$ip = '127.0.0.1';

}else{

	$ip = $_SERVER['REMOTE_ADDR'];
}

$ip     = ip2long($ip);

//将数据插入数据库bbs_details表中
$table = DB_TABLE_DETAIL;
$data_post = [
	'title'    => "$title",
	'content'  => "$content",
	'classid'  => $classid,
	'addtime'  => time(),
	'authorid' => $authorid,	
	'rate'     => $rate,
	'addip'    => $ip,
	'first'    => 1
];
//var_dump($data_post);
$res_post = insert($link , $table , $data_post);
//var_dump($res_post);

/**************加积分*********************/
//查询出用户原积分信息
$num    = 4;
$table  = DB_TABLE_USER;
$fields = 'grade';
$where  = "where uid=$authorid";
$res_grade1 = select($link,$table,$fields,$where);
$grade = $res_grade1[0]['grade'];
//var_dump($res_grade1);
//var_dump($grade);

//给用户加积分
$data   = ['grade' => $grade+$num];
$where  = "uid=$authorid";
$res_grade2 = update($link,$table,$data,$where);

/**************置顶****************************/
//最后发表的置顶
$table = DB_TABLE_CG;
$data  = ['lastpost' => 1];
$where = "cid=$classid";
$last_post = update($link , $table , $data ,$where);
//var_dump($last_post);

if (!$res_post) {
	exit('发表失败');
}

/**************cookie慎用，退出时务必全部清除*************/
//setcookie('id',"$res_post",time()+60*60000,'/');
//var_dump($_COOKIE['id']);
echo '发表成功,积分+'.$num;
/***********************************************/
//自加money字段（）
/*
//查询money
$fields = 'money';
$where = "where id=$res_post";
$data_money = select($link,$table,$fields,$where);
$money = $data_money[0]['money'];
var_dump($money);
//修改money
$data = ['money' => $money];
$res_money = update($link,$table,$data,$where);
*/
/***********************************************/
//跳入个人主页

header('refresh:3;url='.WEB_SITE.'personal.php?id='.$res_post);
mysqli_close($link);